package com.itheima.web.controller.store.company;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.store.Company;
import com.itheima.utils.BeanUtil;
import com.itheima.web.controller.BaseServlet;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @program: mm
 * @description:
 * @author: zhanghz001
 * @create: 2020-11-12 15:37
 **/
@WebServlet("/store/company")
public class CompanyServlet extends BaseServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String method = req.getParameter("operation");
        if ("list".equals(method)) {
            list(req, resp);
        } else if ("save".equals(method)) {
            save(req, resp);
        } else if ("delete".equals(method)) {
            delete(req, resp);
        } else if ("edit".equals(method)) {
            edit(req, resp);
        } else if ("toAdd".equals(method)) {
            toAdd(req, resp);
        } else if ("toEdit".equals(method)) {
            toEdit(req, resp);
        } else {
            System.out.println("没有定义的url");
        }
    }

    private void toEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String id = req.getParameter("id");
        Company company = companyService.findById(id);
        req.setAttribute("company", company);
        req.getRequestDispatcher("/WEB-INF/pages/store/company/update.jsp").forward(req, resp);

    }

    private void toAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("/WEB-INF/pages/store/company/add.jsp").forward(req, resp);

    }

    private void edit(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Company company = BeanUtil.fillBean(req, Company.class, "yyyy-MM-dd");

        companyService.update(company);
        resp.sendRedirect(req.getContextPath() + "/store/company?operation=list");

    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Company company = BeanUtil.fillBean(req, Company.class);
        companyService.delete(company);
        resp.sendRedirect(req.getContextPath() + "/store/company?operation=list");

    }

    private void save(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Company company = BeanUtil.fillBean(req, Company.class, "yyyy-MM-dd");
        companyService.save(company);
        resp.sendRedirect(req.getContextPath() + "/store/company?operation=list");

    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pageStr = req.getParameter("page");
        String sizeStr = req.getParameter("size");

        int page = 1;
        int size = 10;
        if (StringUtils.isNotBlank(pageStr)) {
            try {
                page = Integer.parseInt(pageStr);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (StringUtils.isNotBlank(sizeStr)) {
            try {
                size = Integer.parseInt(sizeStr);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        PageInfo pageInfo = companyService.findAll(page, size);
        req.setAttribute("page", pageInfo);
        //转发不需要带上ctx
        req.getRequestDispatcher("/WEB-INF/pages/store/company/list.jsp").forward(req, resp);

    }
}
